﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SM2TestRunner;
using SM2Utilities;
using ArtOfTest.WebAii.Controls.HtmlControls;

namespace SM2Profile
{
    public class Profile
    {
        public static string strConfirm = "no";

        //Creating new Profile
        public static void createNewProfile()
        {
            string strProfileName = TestDispatcher.TEST_PARAMETER.get("Profile Name");
            strConfirm = TestDispatcher.TEST_PARAMETER.get("Confirm");

            //Load Home Page
            CommonLoader.load("home.aspx");

            if (!ProfileActions.isProfileNameExisted(strProfileName))
            {
                //Click on link "Create New Profile"
                Utilities.GlobalObject.MANAGER.ActiveBrowser.RefreshDomTree();
                HtmlAnchor cphPageContent_lnkCreateProfile = Utilities.GlobalObject.MANAGER.ActiveBrowser.Find.ById<HtmlAnchor>("cphPageContent_lnkCreateProfile");
                SM2Input.Tradition.ClickAnchor(cphPageContent_lnkCreateProfile);

                //Input name of profile
                ProfileActions.actionInputTextOnPopup(strProfileName);

                //Click Create button
                ProfileActions.actionConfirmOnPopup();

                //Verify if the newly created profile is available
                CommonLoader.load("home.aspx");
                if (!ProfileActions.isProfileNameExisted(strProfileName)) throw new Exception("Fail to create new profile [" + strProfileName + "]");
                else Utilities.GlobalObject.LOGGER.log("Newly created profile is available");
            }
            else Utilities.GlobalObject.LOGGER.log("Profile already existed");
        }

        //Copy Profile
        public static void copyProfile()
        {
            string strProfileName = TestDispatcher.TEST_PARAMETER.get("Profile Name");
            string strSourcedProfile = TestDispatcher.TEST_PARAMETER.get("Selected Profile Name");
            strConfirm = TestDispatcher.TEST_PARAMETER.get("Confirm");

            //Load Home Page
            CommonLoader.load("home.aspx");

            //Click on link "Create New Profile"
            Utilities.GlobalObject.MANAGER.ActiveBrowser.RefreshDomTree();
            HtmlAnchor cphPageContent_lnkCreateProfile = Utilities.GlobalObject.MANAGER.ActiveBrowser.Find.ById<HtmlAnchor>("cphPageContent_lnkCreateProfile");
            SM2Input.Tradition.ClickAnchor(cphPageContent_lnkCreateProfile);

            //Input name of profile
            ProfileActions.actionInputTextOnPopup(strProfileName);

            //Check the checkbox
            HtmlDiv cphPageContent_pnlMPAddEditProfile = Utilities.GlobalObject.MANAGER.ActiveBrowser.Find.ById<HtmlDiv>("cphPageContent_pnlMPAddEditProfile");
            HtmlDiv cphPageContent_divNewProfileOptions = cphPageContent_pnlMPAddEditProfile.Find.ById<HtmlDiv>("cphPageContent_divNewProfileOptions");
            HtmlInputCheckBox cphPageContent_chkCopyFrom = cphPageContent_divNewProfileOptions.Find.ById<HtmlInputCheckBox>("cphPageContent_chkCopyFrom");
            SM2Input.Tradition.SelectCheckbox(cphPageContent_chkCopyFrom);

            //Select an existing profile name
            HtmlSelect cphPageContent_ddlProfiles = cphPageContent_divNewProfileOptions.Find.ById<HtmlSelect>("cphPageContent_ddlProfiles");
            SM2Input.Tradition.SelectItemSelectList(cphPageContent_ddlProfiles, strSourcedProfile);

            //Click Create button
            ProfileActions.actionConfirmOnPopup();

            //Verify if the newly created profile is available
            CommonLoader.load("home.aspx");
            if (!ProfileActions.isProfileNameExisted(strProfileName)) throw new Exception("Fail to create new profile [" + strProfileName + "]");
            else Utilities.GlobalObject.LOGGER.log("Copy profile is completed");
        }

        //Delete Profile
        public static void deleteProfile()
        {            
            string strSelectedProfile = TestDispatcher.TEST_PARAMETER.get("Selected Profile Name");
            strConfirm = TestDispatcher.TEST_PARAMETER.get("Confirm");

            //Load Home Page
            CommonLoader.load("home.aspx");

            //Note down the # results in profile and in total
            int intNumberResultsInProfile = ProfileActions.getNumberResultByProfileName(strSelectedProfile);
            int intTotalsBefore = ProfileActions.getTotalCurrentResults();

            //Click Delete
            HtmlControl controlDelete = ProfileActions.getControlDeleteByProfileName(strSelectedProfile);
            controlDelete.Click();
            Utilities.GlobalObject.MANAGER.ActiveBrowser.WaitUntilReady();
            System.Threading.Thread.Sleep(Utilities.GlobalConfig.INT_WAIT_INTERVAL_MILISECOND);

            //Confirm Delete
            ProfileActions.actionConfirmOnPopup();

            //Verify the Totals number was decreased accordingly
            int intTotalsAfter = ProfileActions.getTotalCurrentResults();
            if (intTotalsBefore == (intTotalsAfter + intNumberResultsInProfile)) Utilities.GlobalObject.LOGGER.log("Totals number was decreased accordingly");
            else throw new Exception("Total number was NOT decreased accordingly [" + intTotalsBefore + " != " + intTotalsAfter.ToString() + "+" + intNumberResultsInProfile.ToString() + "]");
        }

        //Rename Profile
        public static void renameProfile()
        {
            string strSelectedProfile = TestDispatcher.TEST_PARAMETER.get("Selected Profile Name");
            string strNewProfileName = TestDispatcher.TEST_PARAMETER.get("Profile Name");
            strConfirm = TestDispatcher.TEST_PARAMETER.get("Confirm");

            //Load Home Page
            CommonLoader.load("home.aspx");

            //Click Rename profile
            HtmlControl controlRename = ProfileActions.getControlRenameByProfileName(strSelectedProfile);
            controlRename.Click();
            Utilities.GlobalObject.MANAGER.ActiveBrowser.WaitUntilReady();
            System.Threading.Thread.Sleep(Utilities.GlobalConfig.INT_WAIT_INTERVAL_MILISECOND);

            //Update name of profile
            ProfileActions.actionInputTextOnPopup(strNewProfileName);

            //Confirm
            ProfileActions.actionConfirmOnPopup();

            //Verify renamed was succeeded
            if (!ProfileActions.isProfileNameExisted(strNewProfileName)) throw new Exception("Fail to rename profile [" + strSelectedProfile + "] to [" + strNewProfileName + "]");
            else Utilities.GlobalObject.LOGGER.log("Rename profile is completed");
        }

        //Load Profile
        public static void loadProfile()
        {
            string strSelectedProfile = TestDispatcher.TEST_PARAMETER.get("Selected Profile Name");

            //Load Home Page
            CommonLoader.load("home.aspx");

            //Click Open profile
           ProfileActions.loadProfile(strSelectedProfile);
        }
    }
}
